package com.huibao.hulibao.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.huibao.hulibao.R;
import com.huibao.hulibao.fragment.LocationFragmentView;
import com.huibao.hulibao.utils.T;

import java.util.ArrayList;

/**
 * Created by lizhaoyong on 2018/5/10.
 */
public class PhotoGvAdapter extends BaseAdapter {
    private ArrayList<Bitmap> al_Bitmaps;
    private Context context;
    private ArrayList<String> al_imgs;
    private int size;

    public PhotoGvAdapter(ArrayList<Bitmap> al_Bitmaps, Context context, ArrayList<String> al_imgs, int size) {
        this.al_Bitmaps = al_Bitmaps;
        this.context = context;
        this.al_imgs = al_imgs;
        this.size = size;

    }

    @Override
    public int getCount() {
        if (al_Bitmaps.size() == size) {
            return al_Bitmaps.size();
        }
        return al_Bitmaps.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        View v;
        if (size > 3) {
            v = LayoutInflater.from(context).inflate(
                    R.layout.item_gv_photo_small, null);
        } else {
            v = LayoutInflater.from(context).inflate(
                    R.layout.item_gv_photo, null);
        }

        ImageView iv_delete = (ImageView) v.findViewById(R.id.iv_gv_delete);
        final ImageView iv_photo = (ImageView) v.findViewById(R.id.iv_gv_photo);
        if (i < al_Bitmaps.size()) {
            Bitmap bitmap = al_Bitmaps.get(i);
            if (bitmap != null) {
                iv_photo.setImageBitmap(al_Bitmaps.get(i));
                iv_delete.setVisibility(View.VISIBLE);
            } else {
                iv_photo.setImageResource(R.mipmap.photo_bg);
                iv_delete.setVisibility(View.GONE);
            }
        }

        // Glide.with(context).load(al_imgs.get(i)).into(iv_photo);


        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // al_Bitmaps.set(position, null);
                if (i < al_Bitmaps.size()) {
                    al_Bitmaps.remove(i);
                    al_imgs.remove(i);
                    notifyDataSetChanged();
                }
            }
        });
        return v;
    }
}
